package com.edwin.websitebe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edwin.websitebe.model.ProductShowType;
import com.edwin.websitebe.service.ProductShowTypeService;
import com.edwin.websitebe.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/productShowType")
public class ProductShowTypeController {

    @Autowired
    private ProductShowTypeService service;

    @GetMapping("/page")
    public ResultVO page(@RequestParam Long page, @RequestParam Long pageSize, @RequestParam(required = false) String typeName) {

        LambdaQueryWrapper<ProductShowType> wrapper = new QueryWrapper<ProductShowType>().lambda()
                .like(Objects.nonNull(typeName), ProductShowType::getTypeName, typeName)
                .orderByAsc(ProductShowType::getSort)
                .orderByDesc(ProductShowType::getCreateTime);
        Page<ProductShowType> p = service.page(Page.of(page, pageSize), wrapper);


        return new ResultVO().success(p);
    }

    @GetMapping("/list")
    public ResultVO list() {
        LambdaQueryWrapper<ProductShowType> wrapper = new QueryWrapper<ProductShowType>().lambda()
                .orderByAsc(ProductShowType::getSort)
                .orderByDesc(ProductShowType::getCreateTime);
        List<ProductShowType> list = service.list(wrapper);
        return new ResultVO().success(list);
    }

    @GetMapping("/getById/{id}")
    public ResultVO getById(@PathVariable("id") Long id) {
        ProductShowType sample = service.getById(id);
        return new ResultVO().success(sample);
    }

    @PostMapping("")
    public ResultVO saveOrUpdate(@RequestBody ProductShowType product) {
        boolean result = service.saveOrUpdate(product);
        return result ? new ResultVO().success() : new ResultVO().fail();
    }

    @DeleteMapping("/delete")
    public ResultVO delete(@RequestParam Long id) {
        boolean result = service.removeById(id);
        return new ResultVO().success();
    }

}
